/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.geergenbein.azw.dao.transcription;

import javax.persistence.EntityManager;
import org.geergenbein.azw.model.transcription.TonedCodePoint;
import org.geergenbein.azw.model.transcription.TonedTranscription;
import org.geergenbein.azw.model.transcription.TranscriptionSystem;
import org.geergenbein.dao.GenericHibernateDAO;

/**
 *
 * @author andrew
 */
public class TonedTranscriptionDAOHibernate extends GenericHibernateDAO<TonedTranscription, Long> {
    
    public TonedTranscriptionDAOHibernate(EntityManager em) {
        super(em);
    }
    
    public TonedTranscription find(TranscriptionSystem transcriptionSystem,
            TonedCodePoint tonedCodePoint) 
    {
        return (TonedTranscription) getSession().
                getNamedQuery("findTonedTranscription").
                setParameter("transcriptionSystem", transcriptionSystem).
                setParameter("tonedCodePoint", tonedCodePoint).
                uniqueResult();
    }
    
    // persistOrUpdate
    // factory/create
    public TonedTranscription loadOrPersist(TonedCodePoint tonedCodePoint,
            TranscriptionSystem transcriptionSystem, String transcription) 
    {
        TonedTranscription tt = null;
        if ((tt = find(transcriptionSystem, tonedCodePoint)) == null) {
            tt = new TonedTranscription(tonedCodePoint, transcriptionSystem, 
                    transcription);
            makePersistent(tt);
            log.debug("Persisted UntonedTranscription: " + tt);
        } else {
            // update???
            log.debug("Found UntonedTranscription: " + tt);
        }
        return tt;
    }
    
}
